# -*- coding: utf-8 -*-

import hashlib
from base import UserCase, Runner, TestSuite


class Case(UserCase):

    def test_my_corp_user_list(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list')
        self.assertEqual(res['code'], 0)


    def test_my_corp_user_list2(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/corp-user/create', {
            'corp': id,
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest()})

        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)

    def test_my_corp_user_list_status(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/corp-user/create', {
            'corp': id,
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest()
        })

        res = self.c.open('/api/corp-user/create', {
            'corp': id,
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest()
        })

        user_id  = res['data']['user_obj']['id']

        user = self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 3)

        res = self.c.open('/api/my-corp-user/delete', {'user': user_id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list', {'status': 'normal'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)

        res = self.c.open('/api/my-corp-user/list', {'status': 'delete'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)


    def test_my_corp_user_option(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/corp-user/create', {
            'corp': id,
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest()})

        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/option')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)


    def test_my_corp_user_create(self):

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list')
        count = res['data']['count']

        p = {
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
            'role': 'normal',
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        res = self.c.open('/api/my-corp-user/list')
        self.assertEqual(res['data']['count'], count + 1)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        p = {
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
            'role': 'admin',
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertNotEqual(res['code'], 0)



    def test_my_corp_user_create2(self):

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list')
        count = res['data']['count']

        p = {
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
            'role': 'admin',
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        p = {
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
            'role': 'admin',
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        res = self.c.open('/api/my-corp-user/list')
        self.assertEqual(res['data']['count'], count + 2)


    def test_my_corp_user_delete(self):

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)


        p = {
            'corp': corp['id'],
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
            'role': 'normal',
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        p2 = {
            'user': user_id,
        }
        res = self.c.open('/api/my-corp-user/delete', p2)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)
        res = self.c.open('/api/session/read')
        self.assertEqual(res['data']['corp'], None)


    def test_my_corp_user_create_mobile(self):

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list')
        count = res['data']['count']

        p = {
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
            'role': 'normal',
            'mobile': self.randnum(11),
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        res = self.c.open('/api/my-corp-user/list', {'user': user_id})
        self.assertEqual(res['data']['itemList'][0]['user_obj']['mobile'], p['mobile'])


    def test_my_corp_user_create_address(self):

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list')
        count = res['data']['count']

        p = {
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
            'role': 'normal',
            'mobile': self.randnum(11),
            'address': self.randhan(100),
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        res = self.c.open('/api/my-corp-user/list', {'user': user_id})
        self.assertEqual(res['data']['itemList'][0]['user_obj']['mobile'], p['mobile'])
        self.assertEqual(res['data']['itemList'][0]['user_obj']['address'], p['address'])


    def test_my_corp_user_create_log(self):

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list', method='GET')
        count = res['data']['count']

        res = self.c.open('/api/my-action-log/list', method='GET')
        action_count = res['data']['count']

        p = {
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
            'role': 'normal',
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        res = self.c.open('/api/my-action-log/list')
        self.assertEqual(action_count, res['data']['count'] - 1)

        res = self.c.open('/api/my-corp-user/list')
        self.assertEqual(res['data']['count'], count + 1)


    def test_my_corp_user_list_as_normal(self):

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        p = {
            'name': self.randhan(3),
            'role': 'normal',
            'password': hashlib.md5(self.randchar(6)).hexdigest(),
            'username': self.randchar(8),
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list')
        self.assertNotEqual(res['code'], 0)


    def test_my_corp_user_revert(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/corp-user/create', {
            'corp': id,
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest()
        })

        res = self.c.open('/api/corp-user/create', {
            'corp': id,
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest()
        })

        user_id  = res['data']['user_obj']['id']

        user = self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 3)

        res = self.c.open('/api/my-corp-user/delete', {'user': user_id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list', {'status': 'normal'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)

        res = self.c.open('/api/my-corp-user/list', {'status': 'delete'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)

        res = self.c.open('/api/my-corp-user/revert', {'user': user_id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user/list', {'status': 'normal'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 3)

        res = self.c.open('/api/my-corp-user/list', {'status': 'delete'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 0)


test_list = [
    'test_my_corp_user_list',
    'test_my_corp_user_list2',
    'test_my_corp_user_list_status',
    'test_my_corp_user_option',
    'test_my_corp_user_create',
    'test_my_corp_user_create2',
    'test_my_corp_user_delete',
    'test_my_corp_user_create_mobile',
    'test_my_corp_user_create_address',
    'test_my_corp_user_create_log',
    'test_my_corp_user_list_as_normal',
    'test_my_corp_user_revert',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



